package com.group01.community.controller;


import com.group01.community.entity.SysDept;
import com.group01.community.entity.SysUser;
import com.group01.community.entity.vo.SysDeptVo;
import com.group01.community.service.SysDeptService;
import com.group01.community.service.SysUserService;
import com.group01.community.utils.DeptTreeUtils;
import com.group01.community.utils.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author guoyuan
 * @since 2025-05-01
 */
@Api
@RestController
@RequestMapping("/sysDept")
public class SysDeptController {
    @Autowired
    private SysDeptService sysDeptService;

    @Autowired
    private SysUserService userService;

//    @ApiOperation("查询所有三级部门（根据部门名称==>左侧导航栏显示的部门权限）")
//    @GetMapping("/dept/{deptName}")
//    public ResponseResult<List<SysDeptVo>> getDeptByname(@PathVariable String deptName){
////        System.out.println(deptName);
//        return ResponseResult.ok(sysDeptService.getDeptsByName(deptName));
//    }

    @ApiOperation("查询所有分级部门(四级部门+模糊查询)")
    @GetMapping("/dept/")
    public ResponseResult<List<SysDeptVo>> getDeptList(String deptName, String status){
        List<SysDeptVo> deptList = sysDeptService.getDeptList(deptName,status);
        //构建树
        List<SysDeptVo> deptLists = new DeptTreeUtils(deptList).buildTree();
        return ResponseResult.ok(deptLists);
    };

    @ApiOperation("根据用户角色查询角色的所有部门信息")
    @GetMapping("/deptTree/")
    public ResponseResult<List<SysDeptVo>> getDeptTreeByRole( SysUser sysUser){
        List<SysDeptVo> info = sysDeptService.getDeptByDeptNameAndRole(sysUser);
        return ResponseResult.ok(info);
    };

    @ApiOperation("新增部门数据")
    @PostMapping ("/dept")
    public ResponseResult<String> addDept(@RequestBody SysDept sysDept){
        LocalDateTime creatTime = LocalDateTime.now();
        sysDept.setCreateTime(creatTime);
        //添加组级列表ancestors
        SysDept dept = sysDeptService.getById(sysDept.getParentId());
        String ancestors = dept.getAncestors();
        sysDept.setAncestors(ancestors + "," + dept.getDeptId());
        sysDeptService.addDept(sysDept);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("更新部门数据")
    @PutMapping("/dept")
    public ResponseResult<String>updateDept(@RequestBody SysDept sysDept){
        LocalDateTime updateTime = LocalDateTime.now();
        sysDept.setUpdateTime(updateTime);
        sysDeptService.updateDept(sysDept);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("删除部门数据（根据部门id）")
    @DeleteMapping ("/dept/{deptId}")
    public ResponseResult<String> deleteDept( @PathVariable Long deptId){
        sysDeptService.deleteDept(deptId);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("根据部门名称查询部门信息")
    @GetMapping ("/depts")
    public ResponseResult<SysDept> getDept(String deptName){
        return ResponseResult.ok(sysDeptService.getDeptByName(deptName));
    }

    @ApiOperation("根据部门id查询成员")
    @GetMapping ("/select/{deptId}")
    public ResponseResult<Boolean> getDeptUser(@PathVariable Long deptId){
        List<SysUser> user = userService.getDeptUserByDeptId(deptId);
        if(user.size() != 0){
            return ResponseResult.ok(false);
        }
            return  ResponseResult.ok(true);

    }

    @ApiOperation("根据角色id查询 岗位权限")
    @GetMapping ("/RoleId")
    public ResponseResult<List<SysDept>>getDeptListByRoleId(String roleId){
        return ResponseResult.ok(sysDeptService.getDeptListByRoleId(roleId));
    }

//    @ApiOperation("根据部门名称模糊查询部门信息")
//    @GetMapping ("/deptInfo")
//    public ResponseResult<List<SysDept>>getDeptListBydeptName(String deptName){
//        List<SysDept> info = sysDeptService.getDeptByDeptName(deptName);
//
//        return ResponseResult.ok(sysDeptService.getDeptListByRoleId(roleId));
//    }
@ApiOperation("用户管理根据部门状态显示")
    @GetMapping("/deptsList")
    public ResponseResult<List<SysDeptVo>> getDeptLists(String deptName, String status){
        List<SysDeptVo> deptList = sysDeptService.getDeptLists(deptName,status);
        //构建树
        List<SysDeptVo> deptLists = new DeptTreeUtils(deptList).buildTree();
        return ResponseResult.ok(deptLists);
    };

//    @ApiOperation("根据部门id查询成员")
//    @GetMapping ("/select/{deptId}")
//    public ResponseResult<String> getDeptUser(@PathVariable Long deptId){
//        List<SysUser> user = userService.getDeptUserByDeptId(deptId);
//        if(user.size() != 0){
//
//            return ResponseResult.ok("error");
//        }
//            return  ResponseResult.ok("ok");
//
//    }

}

